﻿using System;
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;

namespace EPiBoost.Core.Extensions
{
    public static class PageReferenceExtensions
    {
	    public static PageData GetPage(this PageReference pageReference)
	    {
		    if (!PageReference.IsNullOrEmpty(pageReference))
		    {
			    //return EPiServer.DataFactory.Instance.GetPage(pageReference);
			    var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
					return repo.Get<PageData>(pageReference);
		    }
		    return null;
	    }

	    public static T GetPage<T>(this PageReference pageReference) where T : PageData
	    {
		    if (!PageReference.IsNullOrEmpty(pageReference))
		    {
			    // return EPiServer.DataFactory.Instance.Get<T>(pageReference);
			    var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
			    return repo.Get<T>(pageReference);
		    }
		    return default(T);
	    }

	    #region Url

		/// <summary>
		/// Get the external URL for a page
		/// </summary> 
		public static string GetExternalUrl(this PageReference pageReference)
		{
			return pageReference.GetPage().GetExternalUrl();
		}

		/// <summary>
		/// Get the URL for a page
		/// </summary> 
		public static string GetHref(this PageReference pageReference)
		{
			return pageReference.GetPage().GetHref();
		}


		#endregion
		 
	}
}
